<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/static/admin/jsp/include.jsp"%>
<script type="text/javascript">
	var grid;
	var index;
	
	var addFun = function($dialog, $grid, $pjq) {
		var dialog = parent.sy.modalDialog({
			title : '添加新用户',
			width : 500,
			height : 400,
			url : sy.contextPath + '/go?path=user/userEdit',
			buttons : [ {
				text : '保存',
				handler : function() {
					dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
				}
			} ]
		});
	};
	
	var editFun = function($dialog, $grid, $pjq) {
		var rows = grid.datagrid('getSelections');
		if (rows.length != 1) {
			parent.$.messager.w('请选择一条记录进行编辑！');
			return;
		}
		var dialog = parent.sy.modalDialog({
			title : '修改资料',
			width : 600,
			height : 400,
			url : sy.contextPath + '/go?path=user/userEdit&id=' + rows[0].id,
			buttons : [ {
				text : '保存',
				handler : function() {
					dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
				}
			} ]
		});
	};
	
	var creditsFun = function() {
		var rows = grid.datagrid('getSelections');
		if (rows.length != 1) {
			parent.$.messager.w('请选择一条记录进行查看！');
			return;
		}
		var nickName = "";
		if (rows[0].nickName != null && rows[0].nickName != "") {
			nickName = rows[0].nickName;
		}
		var url = sy.contextPath + '/go.do?path=user/userCreditLogList&id=' + rows[0].id;
		var dialog = parent.sy.modalDialog({
			title : '积分日志——' + nickName,
			width:sy.width-150,
			height:sy.height-100,
			//width : 800,
		//	height : 620,
			url : url
		});
		index = dialog.selector.substring(12);
		console.log(dialog.selector.substring(12));
	};

	var pwdFun = function(id) {
		/* var rows = grid.datagrid('getSelections');
		if (rows.length != 1) {
			parent.$.messager.alert('请选择一条记录进行编辑！');
			return;
		} */
		var dialog = parent.sy.modalDialog({
			title : '修改密码',
			width: 400,
			height : 220,
			url : sy.contextPath + '/go.do?path=common/pwdEdit&type=1&id=' + id,
			buttons : [ {
				text : '编辑',
				handler : function() {
					dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
				}
			} ]
		});
	};
	
	$(function() {
		grid = $('#grid').datagrid({
			url : sy.contextPath + '/user/list?user_state=0',
            singleSelect : false,
			columns : [ [ {
                width : $(this).width() * 0.05,
                title : 'id',
                field : 'id',
                checkbox:true,
                align : 'center',
			},{
                width : $(this).width() * 0.05,
                title : 'id',
                field : 'idColums',
                align : 'center',
				formatter : function (v,r,i) {
					return r.id
                }
			},{
				width : $(this).width() * 0.15,
				title : '账号',
				field : 'account',
				align : 'center',
				formatter: function(v, r, i) {
					return UT.addTitle(v);
				}
			},{
				width : $(this).width() * 0.15,
				title : '昵称',
				field : 'nickName',
				align : 'center',
				formatter: function(v, r, i) {
					return UT.addTitle(v);
				}
			},{
                width : $(this).width() * 0.05,
                title : '头像',
                field : 'headImg',
                align : 'center',
                formatter : function(value, row, index) {
                    if (value != null && value != '') {
                        return po.showImg(value, 20, 20);
                    } else {
                        return null;
                    }
                }
            },{
                width : $(this).width() * 0.05,
                title : '参拍数量',
                field : 'pawnLogCnt',
                align : 'center',
            },{
                width : $(this).width() * 0.05,
                title : '有效订单数量',
                field : 'orderCnt',
                align : 'center',
            },{
                width : $(this).width() * 0.05,
                title : '用户等级',
                field : 'vipLs',
            },{
					width : $(this).width() * 0.05,
					title : '积分',
					field : 'credit',
					align : 'center'
			},{
                width : $(this).width() * 0.05,
                title : '是否是vip',
                field : 'isVip',
                align : 'center',
				formatter : function (v,r,i) {
					if(v == 1){
						return 'vip'
					}else{
					    return '普通用户'
					}
                }
            }, {
				width : $(this).width() * 0.05,
				title : '操作',
				field : 'op',
				align : 'center',
				formatter : function(value, row, index) {
					var html = "";
					if(row.userType == 0){
						//当前状态为禁用，点击启用
                        return html += '<a href="javascript:void(0);" onclick="forbidden(1,\'' + row.id + '\')" ><img src="static/admin/images/no.png" title="点击启用" > </img></a>';
					}else if(row.userType == 1){
                        //当前状态为启用，点击禁用
                        return html += '<a href="javascript:void(0);" onclick="forbidden(0,\'' + row.id + '\')" ><img src="static/admin/images/yes.png" title="点击禁用" > </img></a>';
					}
					return html;
				}
			}
			] ]
		});
		

		$('#type').combobox({   
			onSelect: function(rec){   
				grid.datagrid('load',sy.serializeObject($('#searchForm')));
	        } 
		});
	});

	function forbidden(userType, id) {
		if (userType == 1) {

			parent.$.messager.confirm('询问', "确定启用此账号吗？", function(r) {
				if (r) {

					var data = {
						id : id,
                        userType : 1
					};
					SaveData(data);
				}
			});
		} else if (userType == 0) {
			parent.$.messager.confirm('询问', "确定禁用此账号吗？", function(r) {
				if (r) {
					var data = {
						id : id,
						userType : 0
					};
					SaveData(data);
				}
			});
		}
	}
	function SaveData(data) {
		var url = sy.contextPath + '/user/update';
		$.post(url, data, function() {
			grid.datagrid('reload');
		}, 'json');
	}

    /**
	 * 导出excel
     */
    function exportExcel(){
        var url = sy.contextPath + '/user/exportExcel';
        location.href=url;
	}

    //批量送积分
    function upLoadList(){
        var rows = grid.datagrid('getSelections');
        if (rows.length == 0) {
            parent.$.messager.w('请选择！');
            return;
        }
        for(var a in rows){
            if(rows[a].orderState < 3){
                return layer.msg("您选中的订单中有无效交易，请去查看");
            }
        }

                var ids = [];
                for ( var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.id);
                }
                var id = ids.join(',');
                var url = sy.contextPath + '/go?path=/user/userAddCredit&ids=' + id;
                var dialog = parent.sy.modalDialog({
                    title : '指定用户赠送积分',
                    width : 500,
                    height : 300,
                    url : url,
                    buttons : [ {
                        text : '保存',
                        handler : function() {
                            dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
                        }
                    } ]
                });
//                $.post(sy.contextPath + '/order/updateLoadEth', {
//                    id : id
//                }, function(result) {
//
//                    rows.length = 0;//必须，否则有bug
//                    if(result.errorCode == -1){
//                        return layer.msg('保存失败,'+result.errorMsg);
//                    }
//                    grid.datagrid('reload');
//                }, 'json');
    }
</script>
</head>
<body>
	<div id="toolbar">
		<form id="searchForm">
		<div>
			<input type="text"  class="easyui-textbox" name="name" style="width: 150px" prompt="昵称/手机号"/>
			<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-search'" onclick="grid.datagrid('load',sy.serializeObject($('#searchForm')));">过滤</a> 
			<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-search-minus'" onclick="$('#searchForm input').val('');grid.datagrid('load',{});">重置过滤</a>
			<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-address-card'" onclick="upLoadList()">批量加分</a>
		<%--<a href="javascript:void(0);" onclick="exportExcel();" class="button button-red" title="导出excel">导出excel</a>--%>
		</div>
		</form>
		<div class="tbbutton">

		</div>
	</div>
	<table id="grid" data-options="fit:true,border:false"></table>
</body>
</html>